<!--  -->
<template>
  <div id="main">
    <h2>等级提升申请管理</h2>
    <el-button type="primary" style="margin:20px" @click="handleOpen('添加等级提升申请',1)">添加等级提升申请</el-button>
    <!-- <div id="search-card">
      <div id="search-content">
        <el-form v-model="search" :inline="true">

          <el-form-item label="昵称">
            <el-input v-model="search.username" />
          </el-form-item>

          <el-form-item label="所属机构">
            <el-select v-model="search.orgId" placeholder="请选择">
              <el-option
                v-for="o in org"
                :key="o.orgId"
                :value="o.orgId"
                :label="o.orgName"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="用户角色">
            <el-select v-model="search.userGroup" placeholder="请选择">
              <el-option key="1" label="系统管理员" value="1" />
              <el-option key="2" label="代理人" value="2" />
              <el-option key="3" label="机构管理员" value="3" />
              <el-option key="4" label="分支机构管理员" value="4" />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="conditionQuery">查找</el-button>
            <el-button icon="el-icon-refresh" circle @click="fetchData()" />
          </el-form-item>

        </el-form>
      </div>

    </div> -->

    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >

        <el-table-column
          label="序号"
          type="index"
          width="80"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="consoleUser.realname"
          label="申请人"
        />

        <el-table-column
          align="center"
          prop="agentApplicationTitle"
          label="申请标题"
        />

        <el-table-column
          align="center"
          prop="agentApplicationLevel"
          label="申请级别"
        >
          <template slot-scope="Scope">
            <span v-if="Scope.row.agentApplicationLevel==1">1星</span>
            <span v-if="Scope.row.agentApplicationLevel==2">2星</span>
            <span v-if="Scope.row.agentApplicationLevel==3">3星</span>
            <span v-if="Scope.row.agentApplicationLevel==4">4星</span>
            <span v-if="Scope.row.agentApplicationLevel==5">5星</span>
            <span v-if="Scope.row.agentApplicationLevel==6">6星</span>
            <span v-if="Scope.row.agentApplicationLevel==7">7星</span>
            <span v-if="Scope.row.agentApplicationLevel==8">8星</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="agentApplicationStatus"
          label="申请状态"
        >
          <template slot-scope="Scope">
            <span v-if="Scope.row.agentApplicationStatus==0">未审核</span>
            <span v-if="Scope.row.agentApplicationStatus==1" style="color:green">已通过</span>
            <span v-if="Scope.row.agentApplicationStatus==2" style="color:red">驳回</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleOpen('等级提升申请详情',2,Scope.row.agentApplicationId)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5,10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog title="等级提升申请详情" :close-on-click-modal="false" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form ref="agentApplication" :model="agentApplication" label-position="top" :rules="rules">

        <el-form-item v-if="editType==2" prop="agentApplicationName" label="申请人" :label-width="formLabelWidth">
          <el-input v-model="agentApplication.agentApplicationName" autocomplete="off" readonly />
        </el-form-item>

        <el-form-item prop="agentApplicationTitle" label="申请标题" :label-width="formLabelWidth">
          <el-input
            v-model="agentApplication.agentApplicationTitle"
            autocomplete="off"
            maxlength="20"
            show-word-limit
            :readonly="readonly"
          />
        </el-form-item>
        <el-form-item prop="agentApplicationLevel" label="申请级别" :label-width="formLabelWidth">
          <!-- <el-input v-model="level.agentApplicationLevel" autocomplete="off" /> -->
          <el-select v-model="agentApplication.agentApplicationLevel" placeholder="请选择" :disabled="disabled">
            <el-option key="1" label="1星" value="1" />
            <el-option key="2" label="2星" value="2" />
            <el-option key="3" label="3星" value="3" />
            <el-option key="4" label="4星" value="4" />
            <el-option key="5" label="5星" value="5" />
            <el-option key="6" label="6星" value="6" />
            <el-option key="7" label="7星" value="7" />
            <el-option key="8" label="8星" value="8" />
          </el-select>
        </el-form-item>
        <el-form-item prop="agentApplicationReason" label="申请理由" :label-width="formLabelWidth">
          <!-- <el-input v-model="level.agentApplicationReason" autocomplete="off" readonly="true" /> -->
          <el-input v-model="agentApplication.agentApplicationReason" type="textarea" :autosize="{ minRows: 5, maxRows: 100}" maxlength="4000" autocomplete="off" :readonly="readonly" />
        </el-form-item>

        <el-form-item prop="agentApplicationFile" label="附件" :label-width="formLabelWidth">
          <el-upload
            v-if="editType==1"
            class="upload-demo"
            name="files"
            :action="url"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            multiple
            :limit="5"
            :on-exceed="handleExceed"
            :file-list="fileList"
            :on-success="handleAvatarSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>

          <el-upload
            v-if="editType==2"
            class="upload-demo"
            name="files"
            :action="url"
            :on-preview="handlePreview"
            multiple
            :limit="5"
            :file-list="fileList"
            disabled
          />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">取 消</el-button>
        <el-button v-if="editType==1" type="primary" @click="handleAdd('agentApplication')">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { getAgentApplicationByUserId, getAgentApplicationById, addAgentApplication } from '@/api/agentApplication'
export default {
// import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      url: process.env.VUE_APP_BASE_API + 'file/',
      list: [],
      listLoading: false,
      dialogFormVisible: false,
      editType: 1, // 1 添加 2 修改
      search: {},
      mohu: '',
      agentApplication: {},
      formLabelWidth: '200px',
      fileList: [],
      fileUrl: '',
      disabled: false,
      readonly: false,
      rules: {
        agentApplicationTitle: [{ required: true, message: '请输入申请标题', trigger: 'blur' }],
        agentApplicationLevel: [{ required: true, message: '请选择申请级别', trigger: 'change' }],
        agentApplicationReason: [{ required: true, message: '请输入申请理由', trigger: 'blur' }]
      }
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.fetchData()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    handleRemove(file, fileList) {
      console.log('删除', fileList)
      this.fileUrl = ''
      for (var i = 0; i < fileList.length; i++) {
        this.fileUrl = this.fileUrl + fileList[i].response.obj
      }
      console.log(this.fileUrl)
    },
    handlePreview(file) {
      console.log(file)
      window.open(file.response.obj)
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handleAvatarSuccess(res, file) {
      this.fileUrl = this.fileUrl + res.obj
    },
    fetchData() {
      this.listLoading = true
      getAgentApplicationByUserId().then((resp) => {
        this.list = resp.obj
        this.listLoading = false
      })
    },
    handleAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.agentApplication.agentApplicationFile = this.fileUrl
          addAgentApplication(this.agentApplication).then((resp) => {
            this.$notify({
              title: '添加成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.agentApplication = {}
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleOpen(title, type, id) {
      this.agentApplication = {}
      this.fileUrl = ''
      this.fileList = []
      this.dialogFormVisible = true
      this.title = title
      this.editType = type
      if (type === 2) {
        this.readonly = true
        this.disabled = true
        getAgentApplicationById(id).then((resp) => {
          this.agentApplication = resp.obj
          this.agentApplication.agentApplicationName = resp.obj.consoleUser.realname
          this.agentApplication.agentApplicationLevel = String(this.agentApplication.agentApplicationLevel)
          if (resp.obj.agentApplicationFile) { // 后台返回的文件链接
            const a = (resp.obj.agentApplicationFile).split(';')
            a.length -= 1
            if (a.length > 0) {
              a.forEach(item => {
                var obj = {}
                const index = item.lastIndexOf('\/')
                const fileName = item.substring(index + 1, item.length) // 最后的文件名截取出来
                const response = {}
                response.obj = item + ';'
                this.$set(obj, 'name', fileName)
                this.$set(obj, 'response', response) // 修改files里面的结构（name,url）
                this.fileList.push(obj)
              })
            }
          }
        })
      }
    },
    handleClose() {
      this.dialogFormVisible = false
      this.agentApplication = {}
      this.readonly = false
      this.disabled = false
    }
  } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped>
#main{
  margin-left: 5px;
  margin-right: 5px;
}
#table-area{
  margin-left: 10px;
  margin-right: 10px;
}
#search-card {
  line-height: 0px;
  text-align: right;
  width: 100%;
  height: 70px;
  overflow-x: hidden;
}
</style>
